import { ref } from 'vue'

const events = ref({})

export const emit = (event, ...args) => {
    if (events.value[event]) {
        events.value[event].forEach(callback => callback(...args))
    }
}

export const on = (event, callback) => {
    if (!events.value[event]) {
        events.value[event] = []
    }
    events.value[event].push(callback)
}

export const off = (event, callback) => {
    if (events.value[event]) {
        if (callback) {
            events.value[event] = events.value[event].filter(cb => cb !== callback)
        } else {
            delete events.value[event]
        }
    }
}
